<?php

namespace App\Command\ResumeParsing\Service\ParsingSpecial;

class ParsingLagou2 extends ParsingSpecial {
    function __construct($helper) {
        parent::__construct($helper);

        $this->get['template'] = function () {
            $this->listXPath("//templateTag[normalize-space()='templateTagName']");    //[contains(., 'templateTagName')][starts-with(normalize-space(), 'templateTagName')]
            $result = $this->xPath->query();
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };
        //if($result->length < 1)	return '';
        //return $this->func->formatTxt($result[0]->textContent);

        $this->get['id'] = function () {
            $tagName = 'ID:';
            $result = $this->xPath->query("//html/body/table/tr/td/table/tr/td/table/tr/td/table/tr/*[starts-with(normalize-space(), '" . $tagName . "')]");
            if ($result->length < 1) return '';
            return $this->func->preg_get('/(?<=' . $tagName . ')\s*(?<result>.*?)\s*(?=$)/u', $result[0]->textContent, array('groupName' => 'result'));
        };

        $this->get['time'] = function () {
            $tagName = '更新时间：';
            // /html/body/document/body/tbl[2]/tr[1]
            // /html/body/document/body/tbl[1]/tr/tc[2]/p[2]

            //$this->listXPath("//*[starts-with(normalize-space(), '杨珂')]");	//[contains(., 'templateTagName')][starts-with(normalize-space(), 'templateTagName')]
            //$this->listXPath("//html/body/document/body/tbl[1]/tr/tc[2]/*");
            $result = $this->xPath->query("//html/body/document/body/tbl[1]/tr/tc[2]/p[1]/r/t");

            $this->renderNode($result);
            exit();

            for ($i = 0; $i < $result->length; $i++) {
                var_dump($result[$i]);
            }


            $result = $this->xPath->query("//html/body/document/body/tbl[1]/tr/tc[2]/p[2]/*");

            $this->listXPath("//*[starts-with(normalize-space(), '杨珂')]");    //[contains(., 'templateTagName')][starts-with(normalize-space(), 'templateTagName')]

            $result = $this->xPath->query("//html/body/table/tr/*[starts-with(normalize-space(),'" . $tagName . "')]");
            if ($result->length < 1) return '';
            return $this->func->preg_get('/(?<=' . $tagName . ')\s*(?<result>.*?)\s*(?=$)/u', $result[0]->textContent, array('groupName' => 'result'));
        };

        $this->get['name'] = function () {
            $tagName = 'ID:';
            $result = $this->xPath->query("//html/body/table/tr/td/table/tr/td/table/tr/td/table/tr/*[starts-with(normalize-space(), '" . $tagName . "')]/preceding-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->preg_get('/(?<result>.*?(?=\n))/u', $result[0]->textContent, array('groupName' => 'result'));
        };

        $this->get['sex'] = function () {
            $result = $this->xPath->query("//html/body/table/tr/td/table/tr/td/table/tr/td/table/tr/*[contains(., '男') or contains(., '女')]");
            if ($result->length < 1) return '';
            return $this->func->preg_get('/(?<result>[男女])性?/u', $result[0]->textContent, array('groupName' => 'result'));
        };

        $this->get['nation'] = $this->tagNotExist;

        $this->get['identitycard'] = $this->tagNotExist;

        $this->get['mobile'] = function () {
            //$this->listXPath("/html/body/table/tr/td/table/tr/td/table/tr/td/table/tr//table/tr/*[contains(., '17550591321')]");
            $tagName = 'ID:';
            $result = $this->xPath->query("//html/body/table/tr/td/table/tr/td/table/tr/td/table/tr/*[starts-with(normalize-space(), '" . $tagName . "')]/../following-sibling::tr[1]/td[2]/*[contains(., '1')]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['email'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='电子邮件：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['age'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='年龄：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['married'] = $this->tagNotExist;

        $this->get['politic'] = $this->tagNotExist;

        $this->get['degree'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='教育程度：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['industry'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='期望行业：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['position'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='期望职位：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['career'] = $this->tagNotExist;

        $this->get['major'] = $this->tagNotExist;

        $this->get['birthday'] = $this->tagNotExist;

        $this->get['workyear'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='工作年限：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['workarea'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='所在地：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['expectarea'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='期望地点：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['area'] = $this->tagNotExist;

        $this->get['salary'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='目前年薪：']/../../following-sibling::td[1]");
            if ($result->length < 1) return '';
            $resultText = $this->func->formatTxt($result[0]->textContent);
            if (preg_match('/^(?:(?<annual>\d+(?:\.\d+)?\s*[kw万]?)\s*元?\s*(?:(?:\/|每个?)\s*年)?(?!\d*(?:\.\d+)?\s*[kw万]?\s*元?\s*(?:\/|每个?)\s*月)\b)?(?:\s*(?(annual)（)?\s*(?<monthly>\d+(?:\.\d+)?\s*[kw万]?)\s*元?\s*(?:\/|每个?)\s*月\s*(?:(?:乘以|[*×x乘])?\s*(?<monthcount>\d{1,2})\s*个?月份?\b\s*)?(?(annual)）))?(?<!^)/u', $resultText, $matches)) {
                if (isset($matches['annual']) && intval($matches['annual'])) {
                    $this->helper->resumeInfo['salaryfrom'] = $this->helper->resumeInfo['salaryend'] = $this->func->readNum($matches['annual']);
                } elseif (isset($matches['monthly']) && intval($matches['monthly'])) {
                    $this->helper->resumeInfo['salaryfrom'] = $this->helper->resumeInfo['salaryend'] = $this->func->readNum($matches['monthly']) * (isset($matches['monthcount']) ? $matches['monthcount'] : 12);
                }
                if (isset($matches['monthly']) && intval($matches['monthly'])) {
                    $this->helper->resumeInfo['salarymfrom'] = $this->helper->resumeInfo['salarymend'] = $this->func->readNum($matches['monthly']);
                } elseif (isset($matches['annual']) && intval($matches['annual'])) {
                    $this->helper->resumeInfo['salarymfrom'] = $this->helper->resumeInfo['salarymend'] = $this->func->readNum($matches['annual']) / 12;
                }
            }
            return $resultText;
        };

        $this->get['expectsalary'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='期望年薪：']/../../following-sibling::td[1]/p[1]");
            if ($result->length < 1) return '';
            $resultText = $this->func->formatTxt($result[0]->textContent);
            if (preg_match('/^(?:(?<annual>\d+(?:\.\d+)?\s*[kw万]?)\s*元?\s*(?:(?:\/|每个?)\s*年)?(?!\d*(?:\.\d+)?\s*[kw万]?\s*元?\s*(?:\/|每个?)\s*月)\b)?(?:\s*(?(annual)（)?\s*(?<monthly>\d+(?:\.\d+)?\s*[kw万]?)\s*元?\s*(?:\/|每个?)\s*月\s*(?:(?:乘以|[*×x乘])?\s*(?<monthcount>\d{1,2})\s*个?月份?\b\s*)?(?(annual)）))?(?<!^)/u', $resultText, $matches)) {
                if (isset($matches['annual']) && intval($matches['annual'])) {
                    $this->helper->resumeInfo['expectsalaryfrom'] = $this->helper->resumeInfo['expectsalaryend'] = $this->func->readNum($matches['annual']);
                } elseif (isset($matches['monthly']) && intval($matches['monthly'])) {
                    $this->helper->resumeInfo['expectsalaryfrom'] = $this->helper->resumeInfo['expectsalaryend'] = $this->func->readNum($matches['monthly']) * (isset($matches['monthcount']) ? $matches['monthcount'] : 12);
                }
                if (isset($matches['monthly']) && intval($matches['monthly'])) {
                    $this->helper->resumeInfo['expectsalarymfrom'] = $this->helper->resumeInfo['expectsalarymend'] = $this->func->readNum($matches['monthly']);
                } elseif (isset($matches['annual']) && intval($matches['annual'])) {
                    $this->helper->resumeInfo['expectsalarymfrom'] = $this->helper->resumeInfo['expectsalarymend'] = $this->func->readNum($matches['annual']) / 12;
                }
            }
            return $resultText;
        };

        $this->get['school'] = $this->tagNotExist;

        $this->get['nofocuse'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/span[normalize-space()='勿推企业：']/../../following-sibling::td[1]/p[2]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['workinfo'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/b/span[normalize-space()='工作经历']/../../../../../following-sibling::table[1]/tr");
            if ($result->length < 1) return '';
            $boundary = $result[0]->attributes->getNamedItem('style')->value;
            $data = array();
            foreach ($result as $tr) {
                if (!$tr->attributes->getNamedItem('style') || $tr->attributes->getNamedItem('style')->value == $boundary) {
                    if (isset($item)) {
                        $item['comment'] = trim($item['comment']);
                        $data[] = $item;
                    }
                    $item = array('comment' => '');
                    $index = 0;
                }
                switch ($index) {
                    case 0:
                        $tmp = array_values(array_filter(array_map('trim', explode("\n", $tr->textContent))));
                        if (count($tmp) == 2) {
                            $item['time'] = $tmp[0];
                            $item['company'] = $tmp[1];
                        }
                        break;
                    case 1:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent, TRUE) . "\r\n";
                        break;
                    case 2:
                        $tmp = array_values(array_filter(array_map('trim', explode("\n", $tr->textContent))));
                        if (count($tmp) == 2) {
                            $item['duty'] = $tmp[0];
                            $item['salary'] = $tmp[1];
                        }
                        break;
                    case 3:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent, TRUE) . "\r\n";
                        break;
                    case 4:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent) . "\r\n";
                        break;
                    default:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent) . "\r\n";
                        break;
                }
                $index++;
            }
            if ($index > 1) $data[] = $item;
            return $data;
        };

        $this->get['projectinfo'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/b/span[normalize-space()='项目经历']/../../../../../following-sibling::table[1]/tr");
            if ($result->length < 1) return '';
            $boundary = $result[0]->attributes->getNamedItem('style')->value;
            $data = array();
            foreach ($result as $tr) {
                if (!$tr->attributes->getNamedItem('style') || $tr->attributes->getNamedItem('style')->value == $boundary) {
                    if (isset($item)) {
                        $item['comment'] = trim($item['comment']);
                        $data[] = $item;
                    }
                    $item = array('comment' => '');
                    $index = 0;
                }
                switch ($index) {
                    case 0:
                        $tmp = array_values(array_filter(array_map('trim', explode("\n", $tr->textContent))));
                        if (count($tmp) == 2) {
                            $item['time'] = $this->func->formatTxt($tmp[0], TRUE);
                            $item['project'] = $tmp[1];
                        }
                        break;
                    case 1:
                        $tmp = array_values(array_filter(array_map('trim', explode("\n", $this->func->formatTxt($tr->textContent)))));
                        if (count($tmp) == 2) {
                            $item['duty'] = $tmp[1];
                        }
                        break;
                    case 2:
                        $tmp = array_values(array_filter(array_map('trim', explode("\n", $tr->textContent))));
                        if (count($tmp) == 2) {
                            $item['company'] = $tmp[1];
                        }
                        break;
                    case 3:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent) . "\r\n";
                        break;
                    case 4:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent) . "\r\n";
                        break;
                    case 5:
                        $item['comment'] .= $this->func->formatTxt($tr->textContent) . "\r\n";
                        break;
                    default:
                        $item['comment'] .= $this->func->formatTxt($node->textContent) . "\r\n";
                        break;
                }
                $index++;
            }
            if ($index > 1) $data[] = $item;
            return $data;
        };

        $this->get['educationinfo'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/b/span[normalize-space()='教育经历']/../../../../../following-sibling::table");
            if ($result->length < 1) return '';

            $data = array();
            $style = $result[0]->attributes->getNamedItem('style')->value;
            foreach ($result as $table) {
                if (!isset($table->attributes) || !isset($table->attributes->getNamedItem('style')->value) || $table->attributes->getNamedItem('style')->value != $style) break;

                $item = array('comment' => '');
                $index = 0;

                $trArr = $this->xPath->query('tr', $table);

                $tmp = array_values(array_filter(array_map('trim', explode("\n", $trArr[0]->textContent))));
                if (count($tmp) == 2) {
                    $item['school'] = $tmp[0];
                    $item['time'] = trim($tmp[1], '（） ');
                }

                $tmp = array_values(array_filter(array_map('trim', explode("\n", $trArr[1]->textContent))));
                if (count($tmp) == 3) {
                    $item['major'] = $this->func->preg_get('/(?<=专业：).*(?=\s{2,}|$)/u', $tmp[0]);
                    $item['degree'] = $this->func->preg_get('/(?<=学历：).*(?=\s{2,}|$)/u', $tmp[1]);
                    $item['comment'] .= trim($tmp[2]);
                }
                $item['comment'] = trim($item['comment']);
                $data[] = $item;
            }
            return $data;
        };

        $this->get['skillinfo'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/b/span[normalize-space()='语言能力']/../../../../../following-sibling::table[1]");
            if ($result->length < 1) return '';
            return '语言能力：' . $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['awardinfo'] = $this->tagNotExist;

        $this->get['commentinfo'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/b/span[normalize-space()='自我评价']/../../../../../following-sibling::table[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };

        $this->get['otherinfo'] = function () {
            $result = $this->xPath->query("//table/tr/td/p/b/span[normalize-space()='附加信息']/../../../../../following-sibling::table[1]");
            if ($result->length < 1) return '';
            return $this->func->formatTxt($result[0]->textContent);
        };
    }
}
